<template>
  <div class="user-role-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户角色分配</span>
        </div>
      </template>

      <el-form :model="searchForm" inline>
        <el-form-item label="用户ID">
          <el-input v-model="searchForm.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadUserRoles">查询</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="userRoles" style="width: 100%" v-loading="loading">
        <el-table-column prop="userId" label="用户ID" width="250" />
        <el-table-column prop="roleId" label="角色ID" width="150" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" type="danger" @click="removeRole(scope.row.userId, scope.row.roleId)">移除角色</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-divider />

      <div class="assign-section">
        <h3>分配角色给用户</h3>
        <el-form :model="assignForm" inline>
          <el-form-item label="用户ID">
            <el-input v-model="assignForm.userId" placeholder="用户ID" />
          </el-form-item>
          <el-form-item label="角色">
            <el-select v-model="assignForm.roleId" placeholder="选择角色">
              <el-option
                v-for="role in roles"
                :key="role.roleId"
                :label="role.roleName"
                :value="role.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="assignRole">分配角色</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getUserRoles, assignRole as assignRoleApi, removeRole as removeRoleApi } from '@/api/userRole'
import { getRoles } from '@/api/role'

const userRoles = ref([])
const roles = ref([])
const loading = ref(false)
const searchForm = ref({
  userId: ''
})

const assignForm = ref({
  userId: '',
  roleId: ''
})

// 加载用户角色列表
const loadUserRoles = async () => {
  loading.value = true
  try {
    const response = await getUserRoles(searchForm.value.userId || '')
    userRoles.value = response.data
  } catch (error) {
    ElMessage.error('加载用户角色列表失败')
  } finally {
    loading.value = false
  }
}

// 加载角色列表
const loadRoles = async () => {
  try {
    const response = await getRoles()
    roles.value = response.data
  } catch (error) {
    ElMessage.error('加载角色列表失败')
  }
}

// 分配角色
const assignRole = async () => {
  try {
    await assignRoleApi(assignForm.value.userId, assignForm.value.roleId)
    ElMessage.success('角色分配成功')
    
    // 清空表单
    assignForm.value = {
      userId: '',
      roleId: ''
    }
    
    // 重新加载用户角色列表
    loadUserRoles()
  } catch (error) {
    ElMessage.error('角色分配失败')
  }
}

// 移除角色
const removeRole = async (userId, roleId) => {
  try {
    await ElMessageBox.confirm(
      '确认移除这个用户的角色吗？',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    await removeRoleApi(userId, roleId)
    ElMessage.success('角色移除成功')
    loadUserRoles() // 重新加载用户角色列表
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('移除角色失败')
    }
  }
}

onMounted(() => {
  loadRoles()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.user-role-management {
  padding: 20px;
}

.assign-section {
  margin-top: 30px;
}

:deep(.el-divider__text) {
  font-size: 16px;
  font-weight: bold;
}
</style>